import Vue from "vue";
import VueRouter from "vue-router";
// 加载条
import NProgress from "nprogress";
import "nprogress/nprogress.css";

Vue.use(VueRouter);

const routes = [
  {
    path: "",
    component: () => import("@/views/layout/layout.vue"),
    children: [
      {
        path: "/",
        name: "/",
        meta: {
          title: "有余",
        },
        component: () => import("@/views/home/home.vue"),
      },
      {
        path: "article",
        name: "article",
        meta: {
          title: "一点笔记",
        },
        component: () => import("@/views/note/article.vue"),
      },
      {
        path: "note/articleDetail/:id",
        name: "articleDetail",
        component: () => import("@/views/note/articleDetail.vue"),
      },
      {
        path: "note/articleClassify",
        name: "articleClassify",
        component: () => import("@/views/note/articleClassify.vue"),
      },
      {
        path: "note/classifyDetail/:id",
        name: "classifyDetail",
        meta: {
          title: "分类",
        },
        component: () => import("@/views/note/classifyDetail.vue"),
      },
      {
        path: "life",
        name: "life",
        meta: {
          title: "茶余饭后",
        },
        component: () => import("@/views/life/life.vue"),
      },
      {
        path: "aboutMe",
        name: "aboutMe",
        meta: {
          title: "关于我",
        },
        component: () => import("@/views/aboutMe/aboutMe.vue"),
      },
      {
        path: "friend",
        name: "friend",
        meta: {
          title: "朋友们",
        },
        component: () => import("@/views/friend/friend.vue"),
      },
      {
        path: "booklist",
        name: "booklist",
        meta: {
          title: "一个老百姓的自我修养",
        },
        component: () => import("@/views/booklist/booklist.vue"),
      },
      {
        path: "project",
        name: "project",
        meta: {
          title: "开源&项目",
        },
        component: () => import("@/views/project/project.vue"),
      },
      {
        path: "projectDetail/:articleId/:projectId",
        name: "projectDetail",
        component: () => import("@/views/project/projectDetail.vue"),
      },
    ],
  },
];

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
  routes,
});

//加载 进度条配置
NProgress.configure({
  easing: "ease", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});

// 前置守卫
router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.meta.title) document.title = to.meta.title;
  next();
});

// 后置守卫
router.afterEach(() => {
  NProgress.done();
});

export default router;
